<template>
  <view class="loading-box">
    <!-- <view class="submit" @click="chooseImage">点击加载</view> -->
    <image :src="bgimg" class="bg-img"></image>
    <view class="bg"></view>
    <view class="main-box">
      <view class="img">
        <image src="https://jichacha-bt-bak.oss-cn-shenzhen.aliyuncs.com/wxxcx/static/images/loading.gif"></image>
      </view>
      <view class="text">检测中，请稍后</view>
    </view>
  </view>
</template>
<script>
import { HTTP_REQUEST_URL } from '@/config/app.js'
import { scanningDetail, analysisDetail, getGoodsDetail } from '@/api/api.js'
export default {
  name: 'ScansLoading',
  data() {
    return {
      bgimg: uni.getStorageSync('submitImgSrc'),
      // retImg:'https://app.jichacha.vip/upload/e8/cb25d38c784a9658a37fa39595f734.jpg',
      retImg: 'https://app.jichacha.vip/upload/04/04cced5be51e58da8969386ec38675.jpg'
    }
  },
  onLoad(opition) {
    const that = this
    if (opition.SubmtType == 2) {
      that.scanningDetailFun()
    } else {
      let token = uni.getStorageSync('LOGIN_STATUS_TOKEN')
      uni.uploadFile({
        url: HTTP_REQUEST_URL + '/api/newUpload',
        filePath: this.bgimg,
        name: "file",
        header: {
          'Authorization': 'Bearer ' + token
        },
        success(res) {
          let data = JSON.parse(res.data);
          that.retImg = data.data.url
          uni.setStorageSync('ScanImg', that.retImg)
          that.analysisDetailFun()
        }
      })
    }

  },
  methods: {
    analysisDetailFun() {
      const that = this
      console.log(this.retImg)
      analysisDetail({
        image_url: this.retImg,
        souce: true
      }).then((res) => {
        const { data } = res
        uni.setStorageSync('RetData', data)
        setTimeout(() => {
          uni.setStorageSync('url', '/pages/scans/analysis')
          uni.reLaunch({
            url: '/pages/tabbar/index?url=true'
          })

        }, 2000)
      }).catch((e) => {
        uni.$u.toast(typeof e === 'string' ? e : '请检查网络连接');
        uni.navigateBack()
      })
    },
    scanningDetailFun() {
      const that = this
      scanningDetail({
        code: this.bgimg,
        souce: true
      }).then((ress) => {
        getGoodsDetail({
          gcode: ress.data.info.code
        }).then((res) => {
          const { data } = res
          let calorie_num = parseFloat(data.info.calorie_num)
          let sugar_num = parseFloat(data.info.sugar_num)
          data.stage_list.calorie.forEach((item, index) => {
            if (calorie_num > item) {
              if (index == data.stage_list.calorie.length - 1 || calorie_num <= data.stage_list.calorie[index + 1]) {
                console.log(item)
                data.info.calorie_index = index + 1
              }
            }
          })
          data.stage_list.sugar.forEach((item, index) => {
            if (sugar_num > item) {
              if (index == data.stage_list.sugar.length - 1 || sugar_num <= data.stage_list.sugar[index + 1]) {
                data.info.sugar_index = index + 1
              }
            }
          })
          uni.setStorageSync('RetData', data)
          setTimeout(() => {
            let url = "/pages/scans/codeResults"

            if (data.info) {
              if (data.item_list.length || data.info.is_scan) {
                url = "/pages/scans/ScanResult"
              } else {
                if (data.info.name) {
                  url = "/pages/scans/ScanResult"
                  // url = "/pages/scans/codeResult"
                } else {
                  url = "/pages/scans/codeResults?message=暂无商品信息，请联系客服处理"
                }
              }
            }
            uni.setStorageSync('url', url)
            uni.reLaunch({
              url: '/pages/tabbar/index?url=true'
            })
          }, 2000)
        })


      }).catch((e) => {
        // uni.redirectTo({
        // 	url:"/pages/scans/ScanResult"
        // })
        uni.setStorageSync('url', "/pages/scans/codeResults?message=" + e)
        uni.reLaunch({
          url: '/pages/tabbar/index?url=true'
        })
      })
    },
    chooseImage() {
      const that = this
      let token = uni.getStorageSync('LOGIN_STATUS_TOKEN')
      uni.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album'],
        success: function (res) {
          uni.setStorageSync('submitImgSrc', res.tempFilePaths[0])
          that.bgimg = res.tempFilePaths[0]
          uni.uploadFile({
            url: HTTP_REQUEST_URL + '/api/newUpload',
            filePath: that.bgimg,
            name: "file",
            header: {
              'Authorization': 'Bearer ' + token
            },
            success(res1) {
              console.log(res1)
            }
          })
        }
      });
    },
  }

}
</script>
<style lang="scss">
.submit {
  width: 300rpx;
  line-height: 100rpx;
  color: white;
  background-color: #00000050;
  text-align: center;
  position: fixed;
  z-index: 9;
}
.loading-box {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  .bg-img {
    width: 100vw;
    height: 100vh;
    vertical-align: top;
    position: relative;
    z-index: 0;
  }
  .bg {
    width: 100vw;
    height: 100vh;
    position: fixed;
    left: 0;
    top: 0;
    background-color: #000000;
    opacity: 0.5;
    z-index: 1;
  }
  .main-box {
    width: 500rpx;
    position: fixed;
    left: calc(50% - 250rpx);
    top: calc(50% - 300rpx);
    z-index: 2;
    text-align: center;
    line-height: 50rpx;
    color: white;
    font-size: 28rpx;
    font-weight: bold;
    .img {
      width: 500rpx;
      height: 500rpx;
      overflow: hidden;
      image {
        width: 100%;
        height: 100%;
        vertical-align: top;
        display: inline-block;
      }
    }
  }
}
</style>
